#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Security

#End Region

Partial Class Usercontrols_Admin_Security
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Public Property ItemName() As String
        Get
            Return CStr(ViewState("ItemName"))
        End Get

        Set(ByVal Value As String)
            ViewState("ItemName") = Value
        End Set
    End Property

    Public Property IsLoadedAsOption() As Boolean
        Get
            Return GetBooleanFromVS("IsLoadedAsOption")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("IsLoadedAsOption", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsLoadedAsOption Then
            SetPageTitle("Security Management")
        End If

        If IsUserAuthenticated = True Then
            If Me.Page.IsPostBack = False Then
                LoadPage()
                LoadRoleList()
            End If

            ConfigBaseNavCtrls()

            If Me.Page.IsPostBack = False Then

                'Set Default Sort Order and Direction
                Me.sortCriteria = "rolename"
                Me.sortDir = "asc"

                BindDataItems()
            End If

        End If

        Me.uxAdminPanel.Visible = ShowControls(SecurityMask.Editor, True)

    End Sub

#End Region

#Region " Save Data "


#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Public Sub LoadItemName()
        Me.uxSecurityTitleLabel.Text = String.Format("Security settings for {0}", ItemName)
    End Sub

    Private Sub LoadRoleList()
        With Me.uxRolesCheckBoxList
            .DataSource = Roles.GetAllRoles
            .DataBind()
        End With
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim List As New SecurityList
        Dim DT As New DataTable
        Dim DV As New DataView

        DT = List.GetSecurityList(ItemGuid)

        Me.RecordCount = DT.Rows.Count

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxResultsGridView
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddSecurityButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddSecurityButton.Click
        Dim iI As Integer

        For iI = 0 To Me.uxRolesCheckBoxList.Items.Count - 1
            If Me.uxRolesCheckBoxList.Items(iI).Selected = True Then
                Dim Item As SecurityDetail = SecurityDetail.NewSecurityDetail()

                Item.Secureitemid = ItemGuid
                Item.Rolename = Me.uxRolesCheckBoxList.SelectedItem.Text.ToString.Trim
                Item.Save()
            End If
        Next


        BindDataItems()

    End Sub

    Protected Sub uxDeleteSecurityButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeleteSecurityButton.Click
        Dim iI As Integer

        While iI < Me.uxResultsGridView.Rows.Count
            Dim Row As GridViewRow = Me.uxResultsGridView.Rows(iI)
            Dim RL As Label = CType(Row.FindControl("uxSecurityIDLabel"), Label)
            Dim CB As Boolean = CType(Row.FindControl("uxSelectCheckBox"), CheckBox).Checked

            If CB Then

                SecurityDetail.DeleteSecurityDetail(ConvertToGuid(RL))

            End If

            System.Math.Min(System.Threading.Interlocked.Increment(iI), iI - 1)

        End While

        BindDataItems()
    End Sub

    Protected Sub DeleteItemRole(ByVal sender As Object, ByVal e As CommandEventArgs)
        SecurityDetail.DeleteSecurityDetail(ConvertToGuid(e.CommandArgument))

        BindDataItems()
    End Sub

    Protected Sub uxResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxResultsGridView.Sorting
        'Get the Sort by Value from the Sortexpression field on the column
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        'Calls the function to rebuild the datagrid view, with the new sort order
        BindDataItems()
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Protected Function GetUsersForRole(ByVal RoleName As String) As String
        Dim UserList As New StringBuilder
        Dim UserItem As Object

        For Each UserItem In Roles.GetUsersInRole(RoleName)
            UserList.Append(UserItem.ToString & ", ")
        Next

        If UserList.Length > 0 Then
            If Right(UserList.ToString, 2) = ", " Then
                UserList.Length = UserList.Length - 2
            End If
        End If

        Return UserList.ToString

    End Function

#End Region

End Class
